<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>项目中对axios的整体封装 | Jaho’s Blog</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="记录日常学习工作">
    
    <link rel="preload" href="/assets/css/0.styles.5b8fe0a4.css" as="style"><link rel="preload" href="/assets/js/app.6514719e.js" as="script"><link rel="preload" href="/assets/js/2.ea114ffb.js" as="script"><link rel="preload" href="/assets/js/42.72739213.js" as="script"><link rel="preload" href="/assets/js/4.e093c564.js" as="script"><link rel="prefetch" href="/assets/js/10.13203aa5.js"><link rel="prefetch" href="/assets/js/11.099868c4.js"><link rel="prefetch" href="/assets/js/12.c8365e17.js"><link rel="prefetch" href="/assets/js/13.89751e7c.js"><link rel="prefetch" href="/assets/js/14.31f563eb.js"><link rel="prefetch" href="/assets/js/15.08b381b6.js"><link rel="prefetch" href="/assets/js/16.1a3867de.js"><link rel="prefetch" href="/assets/js/17.7dd4e0c6.js"><link rel="prefetch" href="/assets/js/18.907c4597.js"><link rel="prefetch" href="/assets/js/19.fac8aa1a.js"><link rel="prefetch" href="/assets/js/20.73fe0e75.js"><link rel="prefetch" href="/assets/js/21.5c50aa1c.js"><link rel="prefetch" href="/assets/js/22.83d03663.js"><link rel="prefetch" href="/assets/js/23.061e7ac0.js"><link rel="prefetch" href="/assets/js/24.cc7e9011.js"><link rel="prefetch" href="/assets/js/25.fa6c7aef.js"><link rel="prefetch" href="/assets/js/26.39392f70.js"><link rel="prefetch" href="/assets/js/27.204436be.js"><link rel="prefetch" href="/assets/js/28.3f54c157.js"><link rel="prefetch" href="/assets/js/29.799d048c.js"><link rel="prefetch" href="/assets/js/3.6c5d6214.js"><link rel="prefetch" href="/assets/js/30.deed02f3.js"><link rel="prefetch" href="/assets/js/31.5916873b.js"><link rel="prefetch" href="/assets/js/32.b0634144.js"><link rel="prefetch" href="/assets/js/33.734675b8.js"><link rel="prefetch" href="/assets/js/34.20404dc4.js"><link rel="prefetch" href="/assets/js/35.d93b829f.js"><link rel="prefetch" href="/assets/js/36.b0692d8d.js"><link rel="prefetch" href="/assets/js/37.3dffa306.js"><link rel="prefetch" href="/assets/js/38.04649a97.js"><link rel="prefetch" href="/assets/js/39.7dfcdc32.js"><link rel="prefetch" href="/assets/js/40.04914511.js"><link rel="prefetch" href="/assets/js/41.ced85449.js"><link rel="prefetch" href="/assets/js/43.de1cff16.js"><link rel="prefetch" href="/assets/js/44.48e301ae.js"><link rel="prefetch" href="/assets/js/45.870a7650.js"><link rel="prefetch" href="/assets/js/46.657f15b8.js"><link rel="prefetch" href="/assets/js/47.3df1bfdb.js"><link rel="prefetch" href="/assets/js/48.f4e4e774.js"><link rel="prefetch" href="/assets/js/49.632bf611.js"><link rel="prefetch" href="/assets/js/5.6531e181.js"><link rel="prefetch" href="/assets/js/50.a8018493.js"><link rel="prefetch" href="/assets/js/51.cb424f59.js"><link rel="prefetch" href="/assets/js/52.a9f595dc.js"><link rel="prefetch" href="/assets/js/53.cf8a9ec0.js"><link rel="prefetch" href="/assets/js/54.46d774a3.js"><link rel="prefetch" href="/assets/js/55.3574f9b8.js"><link rel="prefetch" href="/assets/js/56.baf60ba5.js"><link rel="prefetch" href="/assets/js/57.af42b559.js"><link rel="prefetch" href="/assets/js/58.9300cb59.js"><link rel="prefetch" href="/assets/js/6.00f437ec.js"><link rel="prefetch" href="/assets/js/7.58ac7166.js"><link rel="prefetch" href="/assets/js/8.1b9c5002.js"><link rel="prefetch" href="/assets/js/9.5c076362.js">
    <link rel="stylesheet" href="/assets/css/0.styles.5b8fe0a4.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Jaho’s Blog" class="logo"> <span class="site-name can-hide">Jaho’s Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/posts/" aria-current="page" class="sidebar-link">写在前面</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>工作问题解决</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/posts/项目问题解决/vuevite.html" class="sidebar-link">vue3+vite用proxy解决跨域及proxy原理解析</a></li><li><a href="/posts/项目问题解决/大文件.html" class="sidebar-link">大文件切片上传和断点续传</a></li><li><a href="/posts/项目问题解决/代码优化.html" class="sidebar-link">实际工作中代码优化</a></li><li><a href="/posts/项目问题解决/组件问题.html" class="sidebar-link">解决组件传值问题</a></li><li><a href="/posts/项目问题解决/动态组件.html" class="sidebar-link">动态组件实践</a></li><li><a href="/posts/项目问题解决/大屏.html" class="sidebar-link">大屏九宫格视频</a></li><li><a href="/posts/项目问题解决/滚动.html" class="sidebar-link">Vue中Element表单定位滚动到校验错误位置</a></li><li><a href="/posts/项目问题解决/红绿灯.html" class="sidebar-link">Promise实现动态红绿灯切换</a></li><li><a href="/posts/项目问题解决/懒加载.html" class="sidebar-link">实现元素懒加载</a></li><li><a href="/posts/项目问题解决/小程序组件.html" class="sidebar-link">微信小程序封装组件</a></li><li><a href="/posts/项目问题解决/axios.html" class="active sidebar-link">项目中对axios的整体封装</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/posts/项目问题解决/docker.html" class="sidebar-link">docker实践(附windows安装下载教程)</a></li><li><a href="/posts/项目问题解决/eltable.html" class="sidebar-link">el-table自定义表头及无数据状态</a></li><li><a href="/posts/项目问题解决/gojs.html" class="sidebar-link">gojs入门学习</a></li><li><a href="/posts/项目问题解决/react踩坑.html" class="sidebar-link">React踩坑</a></li><li><a href="/posts/项目问题解决/小程序踩坑.html" class="sidebar-link">小程序踩坑</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Css</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Nodejs</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Vue</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="项目中对axios的整体封装"><a href="#项目中对axios的整体封装" class="header-anchor">#</a> 项目中对axios的整体封装</h1> <h3 id="接口按功能分类"><a href="#接口按功能分类" class="header-anchor">#</a> 接口按功能分类</h3> <p>一个项目中肯定存在多个不同的功能，各个功能会有对应的接口，为了更方便管理，对项目的接口进行分类导出，后续需要对对应功能模块的接口进行修改或者查找会比较方便</p> <p>声明一个<code>metaData</code>功能的接口文件，将属于该功能下的接口都放到一起，然后抛出</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// service/mapping/metaApi</span>
<span class="token keyword">const</span> metaApi <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token string-property property">'meta-api'</span><span class="token operator">:</span><span class="token punctuation">{</span>
    <span class="token literal-property property">url</span><span class="token operator">:</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>service<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/meta/index</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
    <span class="token literal-property property">type</span><span class="token operator">:</span><span class="token string">'get'</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token string-property property">'meta-api-detail'</span><span class="token operator">:</span><span class="token punctuation">{</span>
    <span class="token literal-property property">url</span><span class="token operator">:</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>service<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/meta/detail</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
    <span class="token literal-property property">type</span><span class="token operator">:</span><span class="token string">'get'</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token string-property property">'meta-api-create'</span><span class="token operator">:</span><span class="token punctuation">{</span>
    <span class="token literal-property property">url</span><span class="token operator">:</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>service<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/meta/create</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
    <span class="token literal-property property">type</span><span class="token operator">:</span><span class="token string">'post'</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> metaApi
</code></pre></div><h3 id="整合统一各个功能接口"><a href="#整合统一各个功能接口" class="header-anchor">#</a> 整合统一各个功能接口</h3> <p>上面只是一个接口模块，接着需要对接口模块进行统一整合，放到一个index文件中，将所有的接口抛出，而不是用到哪个功能引入哪个模块接口。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//service/mapping/index</span>
<span class="token keyword">import</span> metaApi <span class="token keyword">from</span> <span class="token string">'./metaAapi'</span> <span class="token comment">//引入接口模块</span>
<span class="token keyword">import</span> sourceAapi <span class="token keyword">from</span> <span class="token string">'./sourceAapi'</span> 
<span class="token keyword">import</span> textAapi <span class="token keyword">from</span> <span class="token string">'./textAapi'</span> 

<span class="token comment">//引入的模块都是一个个对象，进行解构，统一放到一个全局接口中</span>
<span class="token keyword">const</span> apiMapper <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token operator">...</span>metaApi
    <span class="token operator">...</span>sourceAapi
    <span class="token operator">...</span>textAapi
<span class="token punctuation">}</span>
exprot <span class="token keyword">default</span> apiMapper
</code></pre></div><p>解析出来可以看到apiMapper的具体样子</p> <p><img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f4acb3f2fde24ea89d9233adc3314cb0~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <h3 id="对上面的apimapper的type处理"><a href="#对上面的apimapper的type处理" class="header-anchor">#</a> 对上面的apiMapper的type处理</h3> <p>apiMapper的type主要是对接口的一个类型的备注，虽然都是get类型，但是可能传送参数的配置不同，所以先进行备注，接着通过serviceType文件来对备注进行归类，设置备注对应的请求方式。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//serviceType.js</span>

<span class="token keyword">const</span> get <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token string-property property">&quot;get-body&quot;</span><span class="token operator">:</span><span class="token string">'GET'</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;get-quert&quot;</span><span class="token operator">:</span><span class="token string">'GET'</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;get&quot;</span><span class="token operator">:</span><span class="token string">'GET'</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token keyword">const</span> post <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token string-property property">&quot;post-body&quot;</span><span class="token operator">:</span><span class="token string">'POST'</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;post-quert&quot;</span><span class="token operator">:</span><span class="token string">'POST'</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;post&quot;</span><span class="token operator">:</span><span class="token string">'GET'</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token keyword">const</span> del <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token string-property property">'delete'</span><span class="token operator">:</span> <span class="token string">'DELETE'</span><span class="token punctuation">,</span>
  <span class="token string-property property">'delete-body'</span><span class="token operator">:</span> <span class="token string">'DELETE'</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token operator">...</span>get<span class="token punctuation">,</span>
  <span class="token operator">...</span>post<span class="token punctuation">,</span>
  <span class="token operator">...</span>del<span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="按接口类型进行配置"><a href="#按接口类型进行配置" class="header-anchor">#</a> 按接口类型进行配置</h3> <p>设置一个getconfig函数，进行config配置的设置，传入的是上方接口的类型备注，按照不同的类型设置各类类型的请求头，比如<code>get</code>和<code>getbody</code>虽然都是method都是<code>get</code>，但是<code>config</code>内容不同，get需要对参数进行<code>序列化</code></p> <p>这里我们引入qs插件对参数进行序列化，paramsSerializer不同的配置参数的序列化格式不同</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token literal-property property">data</span><span class="token operator">:</span><span class="token punctuation">{</span>
  <span class="token literal-property property">areaID</span><span class="token operator">:</span><span class="token string">'11'</span><span class="token punctuation">,</span>
  <span class="token literal-property property">area</span><span class="token operator">:</span><span class="token string">'11'</span><span class="token punctuation">,</span>
  <span class="token literal-property property">apiKey</span><span class="token operator">:</span><span class="token string">'11'</span><span class="token punctuation">,</span>
  <span class="token literal-property property">dayList</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token function-variable function">paramsSerializer</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> qs<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">indices</span><span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</code></pre></div><p><img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ef66a47da1a34323acc4f57ba9b171b9~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> typeMap <span class="token keyword">from</span> <span class="token string">&quot;./serviceType&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">export</span>  <span class="token keyword">function</span> <span class="token function">getConfig</span><span class="token punctuation">(</span><span class="token parameter">type<span class="token punctuation">,</span>body<span class="token operator">=</span><span class="token keyword">undefined</span><span class="token punctuation">,</span>query<span class="token operator">=</span><span class="token keyword">undefined</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">let</span> conf <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  <span class="token keyword">switch</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">'get'</span><span class="token operator">:</span>
      conf <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">params</span><span class="token operator">:</span> query<span class="token punctuation">,</span>
        <span class="token function">paramsSerializer</span><span class="token punctuation">(</span><span class="token parameter">params</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> qs<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>params<span class="token punctuation">,</span> <span class="token punctuation">{</span>
                <span class="token literal-property property">arrayFormat</span><span class="token operator">:</span> <span class="token string">'repeat'</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token literal-property property">headers</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token string-property property">'Content-Type'</span><span class="token operator">:</span> <span class="token string">'application/x-www-form-urlencoded; charset=UTF-8'</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token literal-property property">responseType</span><span class="token operator">:</span> <span class="token string">'json'</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">'get-body'</span><span class="token operator">:</span>
      conf <span class="token operator">=</span> <span class="token punctuation">{</span>
          <span class="token literal-property property">params</span><span class="token operator">:</span> query<span class="token punctuation">,</span>
          <span class="token literal-property property">headers</span><span class="token operator">:</span> <span class="token punctuation">{</span>
              <span class="token string-property property">'Content-Type'</span><span class="token operator">:</span> <span class="token string">'application/json'</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span>
          <span class="token literal-property property">responseType</span><span class="token operator">:</span> <span class="token string">'json'</span>
      <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">'postJSON'</span><span class="token operator">:</span>
            conf <span class="token operator">=</span> <span class="token punctuation">{</span>
                <span class="token literal-property property">data</span><span class="token operator">:</span> data<span class="token punctuation">,</span>
                <span class="token literal-property property">headers</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token string-property property">'Content-Type'</span><span class="token operator">:</span> <span class="token string">'application/json'</span><span class="token punctuation">,</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">'download-param'</span><span class="token operator">:</span>
      conf <span class="token operator">=</span> <span class="token punctuation">{</span>
          <span class="token literal-property property">params</span><span class="token operator">:</span> data<span class="token punctuation">,</span>
          <span class="token function">paramsSerializer</span><span class="token punctuation">(</span><span class="token parameter">params</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
              <span class="token keyword">return</span> qs<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>params<span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">arrayFormat</span><span class="token operator">:</span> <span class="token string">'repeat'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span>
          <span class="token literal-property property">headers</span><span class="token operator">:</span> <span class="token punctuation">{</span>
              <span class="token string-property property">'Content-Type'</span><span class="token operator">:</span> <span class="token string">'application/x-www-form-urlencoded; charset=UTF-8'</span><span class="token punctuation">,</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span>
          <span class="token literal-property property">responseType</span><span class="token operator">:</span> <span class="token string">'blob'</span>
      <span class="token punctuation">}</span>
    <span class="token keyword">break</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  conf<span class="token punctuation">.</span>method <span class="token operator">=</span> typeMap<span class="token punctuation">[</span>type<span class="token punctuation">]</span>
  <span class="token keyword">return</span> conf
<span class="token punctuation">}</span>
</code></pre></div><p>最后method从typeMap中取出，也就是上一节的内容，最后将整个完整的config返回</p> <h3 id="axios请求配置-拦截器-基本配置"><a href="#axios请求配置-拦截器-基本配置" class="header-anchor">#</a> axios请求配置(拦截器，基本配置)</h3> <p>接着这边是实例，引入接口功能集成、类型、配置</p> <p>设置接口的超时时间，configJWT是对请求头增加token标识</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> reqUrlMap <span class="token keyword">from</span> <span class="token string">'./mapping'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> serviceType <span class="token keyword">from</span> <span class="token string">'./serviceType'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span>getConfig<span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'./serviceConfig'</span>

axios<span class="token punctuation">.</span>defaults<span class="token punctuation">.</span>timeout <span class="token operator">=</span> <span class="token number">500000</span><span class="token punctuation">;</span>
axios<span class="token punctuation">.</span>defaults<span class="token punctuation">.</span>baseURL <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> $axios <span class="token operator">=</span> axios<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> axiosXhr <span class="token operator">=</span> axios<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">configJWT</span><span class="token punctuation">(</span><span class="token parameter">config</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> token <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">this is my token</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
  config<span class="token punctuation">.</span>headers<span class="token punctuation">[</span><span class="token string">'Authorization'</span><span class="token punctuation">]</span> <span class="token operator">=</span> token<span class="token punctuation">;</span>
  <span class="token keyword">return</span> config<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="请求拦截器"><a href="#请求拦截器" class="header-anchor">#</a> 请求拦截器</h3> <p>使用interceptors.request进行请求拦截，对config重新设置，并且拦截器处发生错误会进行抛错</p> <div class="language-js extra-class"><pre class="language-js"><code>$axios<span class="token punctuation">.</span>interceptors<span class="token punctuation">.</span>request<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>
  <span class="token parameter">config</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">let</span> configGhost <span class="token operator">=</span> <span class="token function">configJWT</span><span class="token punctuation">(</span>config<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> configGhost<span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token parameter">error</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token function">msg</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
          <span class="token literal-property property">message</span><span class="token operator">:</span> <span class="token string">'请求异常，请联系管理员'</span><span class="token punctuation">,</span>
          <span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">'error'</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> Promise<span class="token punctuation">.</span><span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="抛出axios请求实例"><a href="#抛出axios请求实例" class="header-anchor">#</a> 抛出axios请求实例</h3> <p>抛出一个异步函数接收，如果请求没有传入alias别名，则返回一个空对象，通过alias别名在mapping中获取接口类型最后把所有配置集成请求axios，这里的axios返回的是一个promise，所以这里用await便于我们请求doRequest请求可以用到then和catch</p> <div class="language-js extra-class"><pre class="language-js"><code>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token keyword">async</span> <span class="token function">doRequest</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
        alias <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">,</span>
        query<span class="token punctuation">,</span>
        body<span class="token punctuation">,</span>
        params
    <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> cfg<span class="token punctuation">;</span>
        <span class="token keyword">let</span> url<span class="token punctuation">;</span>
        <span class="token keyword">let</span> type<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>alias<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        type <span class="token operator">=</span> reqUrlMap<span class="token punctuation">[</span>alias<span class="token punctuation">]</span><span class="token punctuation">.</span>type<span class="token punctuation">;</span>
        url <span class="token operator">=</span> reqUrlMap<span class="token punctuation">[</span>alias<span class="token punctuation">]</span><span class="token punctuation">.</span>url
        url <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">api/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>url<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
        cfg <span class="token operator">=</span> <span class="token punctuation">{</span> 
            url<span class="token punctuation">,</span>
            <span class="token operator">...</span><span class="token function">getConfig</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> query<span class="token punctuation">,</span> body<span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">await</span> <span class="token function">$axios</span><span class="token punctuation">(</span>cfg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="请求例子"><a href="#请求例子" class="header-anchor">#</a> 请求例子</h3> <p>最后我们发送一个请求，发送的请求配置如下</p> <div class="language-js extra-class"><pre class="language-js"><code>http<span class="token punctuation">.</span><span class="token function">doRequest</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token literal-property property">alias</span><span class="token operator">:</span><span class="token string">'aa-api'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">data</span><span class="token operator">:</span><span class="token punctuation">{</span>
      <span class="token literal-property property">areaID</span><span class="token operator">:</span><span class="token string">'11'</span><span class="token punctuation">,</span>
      <span class="token literal-property property">arr</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">22</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>token设置发送正常
<img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/26e96f6908a2485ba75ba3efd9a15bd9~tplv-k3u1fbpfcp-watermark.image?" alt="image.png">
但是我们看到返回的参数是嵌套了多层的，所以需要使用一个响应拦截器对responce进行处理</p> <p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e1b42f571c6642d99d3e881b8b4fbe27~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <h3 id="响应拦截"><a href="#响应拦截" class="header-anchor">#</a> 响应拦截</h3> <p>本来响应返回的是respone,现在我们返回的是responce.data，这样方便我们在请求处理返回内容的时候可以少写一层</p> <div class="language-js extra-class"><pre class="language-js"><code>$axios<span class="token punctuation">.</span>interceptors<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>
    <span class="token parameter">response</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> response<span class="token punctuation">.</span>data
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token parameter">error</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token function">msg</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
            <span class="token literal-property property">message</span><span class="token operator">:</span> <span class="token string">'请求异常，请联系管理员'</span><span class="token punctuation">,</span>
            <span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">'error'</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> Promise<span class="token punctuation">.</span><span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3af50fe5f8184deca6e673c21a6f795a~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-01-04 10:56:12</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/posts/项目问题解决/小程序组件.html" class="prev">
        微信小程序封装组件
      </a></span> <span class="next"><a href="/posts/项目问题解决/docker.html">
        docker实践(附windows安装下载教程)
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.6514719e.js" defer></script><script src="/assets/js/2.ea114ffb.js" defer></script><script src="/assets/js/42.72739213.js" defer></script><script src="/assets/js/4.e093c564.js" defer></script>
  </body>
</html>
